package com.travelsmart.client.apresentacao.view.subview;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.travelsmart.client.apresentacao.controller.CommandController;
import com.travelsmart.client.apresentacao.controller.Observer;
import com.travelsmart.client.apresentacao.i18n.I18nMessages;
import com.travelsmart.client.apresentacao.view.BaseView;
import com.travelsmart.client.apresentacao.widget.FormFlexTable;
import com.travelsmart.client.apresentacao.widget.FormHTML;
import com.travelsmart.client.bean.to.FilterInfoHotel;
import com.travelsmart.client.bean.to.FilterPesquisaHotel;
import com.travelsmart.client.bean.to.HotelTO;
import com.travelsmart.client.bean.to.ParametroOrdenacao;

public class ViewTableHotel extends BaseView implements Observer{
	
	private List<HotelTO> hoteis;
	private CommandController<FilterInfoHotel> handler1;
	private CommandController<ParametroOrdenacao> handler2;
	private final FilterInfoHotel filterInfoHotel = new FilterInfoHotel();
	private final FormFlexTable flexTableHotel = new FormFlexTable(
			new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					Cell cell = flexTableHotel.getCellForEvent(event);
					int i = cell.getRowIndex() - 1;
					if (i >= 0) {// se clicar no header n�o ativar handler
						HotelTO to = hoteis.get(i);
						filterInfoHotel.setCodeHotel(to.getCodigo());
						filterInfoHotel.setLinkSelection(to.getLinkSelection());
						handler1.handleSuccess(filterInfoHotel);
					}
				}
			},
			new HTML(""),
			new FormHTML(I18nMessages.INSTANCE.nome_hotel(), new ClickHandler() {
				
				boolean asc = true;
				@Override
				public void onClick(ClickEvent event) {
					handler2.handleSuccess(new ParametroOrdenacao("nome", asc));
					asc = !asc;
				}
			}), new FormHTML(I18nMessages.INSTANCE.qualidade(), new ClickHandler() {
				boolean asc = true;
				@Override
				public void onClick(ClickEvent event) {
					handler2.handleSuccess(new ParametroOrdenacao("qualidade", asc));
					asc = !asc;
				}
			}), new FormHTML(I18nMessages.INSTANCE.preco_medio_diaria(), new ClickHandler() {
				boolean asc = true;
				@Override
				public void onClick(ClickEvent event) {
					handler2.handleSuccess(new ParametroOrdenacao("precoMedio", true));
					asc = !asc;
				}
			}));

	public ViewTableHotel(HTML nome, final CommandController<FilterInfoHotel> handler1, final CommandController<ParametroOrdenacao> handler2) {
		super(nome);
		this.handler1 = handler1;
		this.handler2 = handler2;
		initWidget(flexTableHotel);
	}
	
	public void exec(List<HotelTO> hoteis) {
		this.hoteis = hoteis;
		flexTableHotel.clearGrid();
		this.hoteis = hoteis;
		for (int i = 0; i < hoteis.size(); i++) {
			HotelTO hotel = hoteis.get(i);
			flexTableHotel.addRow(
					i,
					String.valueOf(i),
					"<img alt=\"" + hotel.getNome() + "\" src=\"" + hotel.getLinkImage() + "\" id=\"\" class=\"hotel_picture\">",
					"<label id='" + hotel.getNome() + "'>" + hotel.getNome() + "</label>", 
							hotel.getQualidade().equals("0.0")? I18nMessages.INSTANCE.sem_avaliacao() : hotel.getQualidade(), 
									hotel.getPrecoMedioString().equals("0.00") ? I18nMessages.INSTANCE.nao_informado() : hotel.getPrecoMedioString());
		}
	}

	@Override
	public void update(Object arg) {
		if(arg instanceof List){
			exec((List<HotelTO>) arg);
		}else if(arg instanceof FilterPesquisaHotel){
			FilterPesquisaHotel filter= (FilterPesquisaHotel)arg;
			filterInfoHotel.setArrivalDate(filter.getEntrada());
			filterInfoHotel.setDepartureDate(filter.getSaida());
		}
	}

}

